﻿@page "/regcontrol"
@using BootstrapBlazor.Components
@using TrackSystem.Entities.Extensions

<div class="row g-3">
    <div class="col-6 col-sm-4 col-md-3 col-lg-auto">
        <Button OnClick="@BtnInitRegs" Color="Color.Primary">载入寄存器</Button>
    </div>

    <div class="col-6 col-sm-4 col-md-3 col-lg-offset-3">
        <Button OnClick="@BtnUnloadRegs" Color="Color.Info">卸载寄存器</Button>
    </div>

    <div class="col-6 col-sm-4 col-md-3 col-lg-auto">
        <BootstrapInput TValue="string" Color="Color.Info" @bind-Value="@pageOptions.Filter"/>
    </div>
    <div class="col-6 col-sm-4 col-md-3 col-lg-auto">
        <Button> <i class="bi bi-search"></i></Button>
    </div>

</div>


@if (PagedRegs is not null && PagedRegs.Any())
{
    <Divider></Divider>
    <div class="row g-3">
        @foreach (var reg in PagedRegs)
        {
            Color clr = reg.Value.DataType switch
            {
                DataTypeEnum.DOUBLE => Color.Danger,
                DataTypeEnum.INTEGER => Color.Primary,
                DataTypeEnum.BOOLEAN => Color.Dark,
                DataTypeEnum.STRING => Color.Info,
                _ => Color.Primary
            };

            <div class="col-12 col-sm-6 col-lg-4 col-xl-3">
                <Card Color="@clr">
                    <HeaderTemplate>
                        @reg.Key: @reg.Value
                    </HeaderTemplate>
                    <BodyTemplate>
                        @if (reg.Value.DataType == DataTypeEnum.BOOLEAN)
                        {
                            <Switch OnColor="Color.Success" OnText="开启" OffText="关闭" @bind-value="@reg.Value.bVal"
                                    OnValueChanged = "@((a) => OnBtnSwitch(reg.Key, a))" />
                        }
                        else if (reg.Value.DataType == DataTypeEnum.INTEGER)
                        {
                            <BootstrapInputNumber OnColor="Color.Success" @bind-value="@reg.Value.iVal" />
                        }
                        else if (reg.Value.DataType == DataTypeEnum.DOUBLE)
                        {
                            <BootstrapInputNumber OnColor="Color.Success" @bind-value="@reg.Value.dVal" />
                        }
                        else if (reg.Value.DataType == DataTypeEnum.STRING)
                        {
                            @* <Textarea OnColor="Color.Success" IsDisabled="true" @bind-value="@reg.Value.sVal" /> *@
                        }
                    </BodyTemplate>
                </Card>
            </div>
        }
    </div>

    <Divider/>
    <Pagination PageCount="@pageOptions.TotalPages" OnPageLinkClick="@OnPageClick" Alignment="Alignment.Center"/>
}
<Divider />
<p> @Message </p>
